<template>
  <el-card style="max-width: 99%">
    <template #header>
      <div class="card-header">
        <span>酒店管理</span>
<!--        <el-button type="primary" style="height: 36px;float: right" @click="openTc(1)">添加酒店</el-button>-->
      </div>
    </template>
    <el-row style="margin-bottom: 20px">
      <el-col :span="5">
        <el-form :model="form" style="max-width: 200px;margin-left: 25px">
          <el-form-item label="酒店名称">
            <el-input v-model="name" placeholder="请输入酒店品牌" />
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="19">
        <el-button type="primary" @click="cx()">搜索</el-button>
        <el-button @click="cz()">重置</el-button>
      </el-col>
    </el-row>
    <el-table :data="list" border style="margin-bottom: 15px">
      <el-table-column prop="brand" label="酒店品牌" width="180" />
      <el-table-column prop="areaName" label="城市" width="180" />
      <el-table-column prop="basicInformation" label="基础信息" />
      <el-table-column prop="phone" label="联系电话" />
      <el-table-column prop="rank" label="酒店类型">
        <template #default="scope">
          <el-tag v-if="scope.row.rank==1" type="info">经济酒店</el-tag>
          <el-tag v-if="scope.row.rank==2" type="success">特惠酒店</el-tag>
          <el-tag v-if="scope.row.rank==3" type="warning">星级酒店</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template #default="scope">
          <el-button :icon="Edit" circle @click="openTc(2,scope.row.id)"/>
          <el-button :icon="SemiSelect" circle  @click="open(scope.row.id)"/>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 0 auto;width: 300px">
      <el-pagination @current-change="handleCurrentChange"
          size="small" background layout="prev, pager, next" :total="count" :page-size=pageSize class="mt-4"/>
    </div>
  </el-card>
  <el-dialog v-model="dialogFormVisible" :before-close="qx" :title="this.type==1?'添加酒店':'编辑'" width="500">
    <el-form :model="pubManageForm" style="margin-right: 60px;margin-left: 60px">
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="pubManageForm.brand" autocomplete="off" />
      </el-form-item>
      <el-form-item label="城市" prop="areaId">
        <el-select v-model="pubManageForm.areaId" placeholder="--请选择--">
          <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="基础信息" prop="basicInformation">
        <el-input v-model="pubManageForm.basicInformation" autocomplete="off" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="pubManageForm.phone" autocomplete="off" />
      </el-form-item>
      <el-form-item label="酒店类型" prop="rank">
        <el-select v-model="pubManageForm.rank" placeholder="请选择酒店类型" style="width: 100%">
          <el-option label="经济酒店" value="1" />
          <el-option label="特惠酒店" value="2" />
          <el-option label="星级酒店" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="酒店图片">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8888/pubManage/upload-img"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
        >
          <img v-if="pubManageForm.pubPath" :src="pubManageForm.pubPath" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="qx">取消</el-button>
        <el-button type="primary" @click="qd()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {selectAllPubManage,deletePubManage,addPubManage,updatePubManage,selectPubManageById,selectProvince} from "@/api/vue3.js"
import {ElMessage, ElMessageBox} from "element-plus";
import {Edit, SemiSelect} from "@element-plus/icons-vue";
export default {
  name: 'Hotel1',
  computed: {
    SemiSelect() {
      return SemiSelect
    },
    Edit() {
      return Edit
    }
  },
  data(){
    return{
      dialogFormVisible: false,//对话框显示
      type:0,//对话框类型
      name:'',//景点名称
      list:[],//景点列表
      count:0,//总条数
      pageSize:5,//每页条数
      pageNum:1,//当前页码
      pubManageForm: {
        pubPath:''
      },
      areaList:[],//地域列表
      cid:'',//地域id
    }
  },
  mounted() {
    this.selectList()
    this.dq()
  },
  methods:{
    dq(){
      selectProvince(this.cid).then(res=>{
        console.log(res)
        this.areaList=res.data.data
      })
    },
    handleAvatarSuccess(file) {//上传成功
      this.pubManageForm.pubPath = file.data;
      console.log(this.pubManageForm.pubPath)
    },
    beforeAvatarUpload(file) {//上传之前
      const isJPG = file.type === 'image/jpeg' || file.type=='image/png';
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return isJPG && isLt2M;
    },
    cx(){//查询
      this.name
      this.selectList()
    },
    cz(){//重置
      this.name=''
      this.selectList()
    },
    selectList(){
      selectAllPubManage(this.name,this.pageNum,this.pageSize).then(res=>{
        this.list=res.data.data.list
        this.count=res.data.data.count
      })
    },
    handleCurrentChange(val) {//当前页码改变的事件
      this.pageNum = val;
      this.selectList()
    },
    //打开弹窗
    openTc(type,id){
      this.dq()
      this.type= type
      this.dialogFormVisible= true
      if (this.type==2){
        selectPubManageById(id).then(res=>{
          // console.log(res)
          this.pubManageForm=res.data.data.data
        })

      }
    },
    open(id) {//删除弹窗
      ElMessageBox.confirm(
          '是否删除该条信息',
          '消息提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      )
          .then(() => {
            deletePubManage(id).then(res=>{
              if(res.data.data.code==200){
                this.selectList()
                ElMessage({
                  type: 'success',
                  message: '删除已完成',
                })
              }else {
                ElMessage({
                  type: 'error',
                  message: '删除失败',
                })
              }
            })
          })
          .catch(() => {
            ElMessage({
              type: 'info',
              message: '删除已取消',
            })
          })
    },
    qd(){
      if(this.type==1){
        addPubManage(this.pubManageForm).then(res=>{
          // console.log(res)
          if(res.data.data.code==200){
            this.selectList()
            this.dialogFormVisible=false
            this.pubManageForm={}
            ElMessage({
              type: 'success',
              message: '添加已完成',
            })
          }else {
            this.dialogFormVisible=false
            ElMessage({
              type: 'error',
              message: '添加失败',
            })
          }
        })
      }
      if(this.type==2){
        updatePubManage(this.pubManageForm).then(res=> {
          if (res.data.data.code == 200) {
            this.selectList()
            this.dialogFormVisible = false
            ElMessage({
              type: 'success',
              message: '修改已完成',
            })
          } else {
            this.dialogFormVisible = false
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
      }
    },
    qx(){
      this.pubManageForm={}
      this.dialogFormVisible=false
      this.value=''
    }
  }
}
</script>

<style>
.el-card{
  width: 100%;
  height: 600px;
  background-color: white;
  margin-top: 20px;
  border-radius: 30px;
}
.el-table{
  margin: 0 auto;
  width: 96%;
}



.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>